﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Management.Automation;
using Microsoft.ApplicationServer.Caching.AdminApi;
using Microsoft.ApplicationServer.Caching.Commands;
using Microsoft.ApplicationServer.Caching;
using System.Threading.Tasks;
using System.Threading;

namespace AppFabricAdministrator
{
    public partial class CacheDetails : UserControl
    {
        private static string DetailsCmd = @"Get-CacheConfig -CacheName {0}";
        private static string RegionsCmd = @"Get-CacheRegion -CacheName {0}";
        private static string StatisticsCmd = @"Get-CacheStatistics -CacheName {0}";

        NamedCaches ncs;
        public CacheDetails()
        {
            InitializeComponent();
            ncs = NamedCaches.GetCurrentNamedCaches(false);
            ncs.Dock = DockStyle.Fill;
            this.scNCB.Panel1.Controls.Clear();
            this.scNCB.Panel1.Controls.Add(ncs);
        }

        private void btnGetCacheDetails_Click(object sender, EventArgs e)
        {
            //ReSet

            this.tbDetails.Text = string.Empty;
            this.tbStatistics.Text = string.Empty;
            this.tbRegions.Text = string.Empty;

            var selectedNC = ncs.SelectedNamedCache;
            //Task<string> t1= Task.Factory.StartNew<string>(() => SetCacheDetails(selectedNC));
            //Task<string> t2=Task.Factory.StartNew<string>(() => SetCacheStatistics(selectedNC));
            //Task<string> t3= Task.Factory.StartNew<string>(() => SetCacheRegiions(selectedNC));
            //this.tbDetails.Text = t1.Result;
            //this.tbStatistics.Text = t2.Result;
            //this.tbRegions.Text = t3.Result;

            this.tbDetails.Text = SetCacheDetails(selectedNC);
            this.tbStatistics.Text = SetCacheStatistics(selectedNC);
            this.tbRegions.Text = SetCacheRegiions(selectedNC);

        }

        private string SetCacheDetails(string selectedNC)
        {
            bool isSuccess = false;
            return ScriptHelper.RunNewScript(string.Format(DetailsCmd, selectedNC), "", CacheConfigResult, out isSuccess, false);
        }
        private string SetCacheStatistics(string selectedNC)
        {
            bool isSuccess = false;
            return ScriptHelper.RunNewScript(string.Format(StatisticsCmd, selectedNC), "", CacheStatisticsResult, out isSuccess, false);
        }
        private string SetCacheRegiions(string selectedNC)
        {
            bool isSuccess = false;
            return ScriptHelper.RunNewScript(string.Format(RegionsCmd, selectedNC), "", CacheRegionResult, out isSuccess, false);
        }
        public static string CacheConfigResult(IEnumerable<PSObject> results)
        {
            StringBuilder resultString = new StringBuilder();
            foreach (var res in results)
            {
                CacheConfig config = (CacheConfig)res.BaseObject;
                resultString.AppendLine(string.Format("EvictionType         :{0}", config.EvictionType));
                resultString.AppendLine(string.Format("IsExpirable          :{0}", config.IsExpirable));
                resultString.AppendLine(string.Format("Notification         :{0}", config.NotificationsEnabled));
                resultString.AppendLine(string.Format("Secondaries          :{0}", config.Secondaries));
                resultString.AppendLine(string.Format("Time To Live         :{0} mins", config.TimeToLive));
                resultString.AppendLine(string.Format("ReadThroughEnabled   :{0}", config.ReadThroughEnabled));
                resultString.AppendLine(string.Format("WriteBehindEnabled   :{0}", config.WriteBehindEnabled));
            }
            return resultString.ToString();
        }
        public static string CacheStatisticsResult(IEnumerable<PSObject> results)
        {
            StringBuilder resultString = new StringBuilder();
            foreach (var res in results)
            {
                NamedCacheStats sts = (NamedCacheStats)res.BaseObject;
                resultString.AppendLine(string.Format("Size              : {0}", sts.Size));
                resultString.AppendLine(string.Format("ItemCount         : {0}", sts.ItemCount));
                resultString.AppendLine(string.Format("RegionCount       : {0}", sts.RegionCount));
                resultString.AppendLine(string.Format("RequestCount      : {0}", sts.RequestCount));
                resultString.AppendLine(string.Format("ReadRequestCount  : {0}", sts.ReadRequestCount));
                resultString.AppendLine(string.Format("WriteRequestCount : {0}", sts.WriteRequestCount));
                resultString.AppendLine(string.Format("MissCount         : {0}", sts.MissCount));
                resultString.AppendLine(string.Format("IncomingBandwidth : {0}", sts.IncomingBandwidth));
                resultString.AppendLine(string.Format("OutgoingBandwidth : {0}", sts.OutgoingBandwidth));
            }
            return resultString.ToString();
        }
        public static string CacheRegionResult(IEnumerable<PSObject> results)
        {
            StringBuilder resultString = new StringBuilder();
            foreach (var res in results)
            {
                RegionInfo ri = (RegionInfo)res.BaseObject;
                resultString.AppendLine(string.Format("{0}:{1}", ri.RegionName, ri.Role));
            }
            return resultString.ToString();
        }
    }
}
